<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    //如果get请求，则查询类别，并展示
    $con = mysqli_connect('localhost', 'root', 'root');
    if (!$con) {
        die('连接失败' . mysqli_connect_error());
    }
    mysqli_select_db($con, 'blog');
    mysqli_set_charset($con, 'utf8');
    $sql = "select * from blog_type order by type_id desc";
    $result = mysqli_query($con, $sql);
    $type_list = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $type_list[] = $row;
    }
    mysqli_close($con);
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //如果post请求，则获取表单的值，并保存到数据库
    //获取表单的值
    $title = $_POST['title'];
    $content = $_POST['content'];
    $blogtype_id = $_POST['blogtype_id'];
    $con = mysqli_connect('localhost', 'root', 'root');
    if (!$con) {
        die('连接失败' . mysqli_connect_error());
    }
    mysqli_select_db($con, 'blog');
    mysqli_set_charset($con, 'utf8');
    $sql = "insert into blog(title,content,blogtype_id,click,create_time) 
values('$title','$content',$blogtype_id,0,now())";
    $result = mysqli_query($con, $sql);
    if (!$result) {
        echo '保存失败' . mysqli_error($con);
    } else {
        header('Location:blog_list.php');
    }

    mysqli_close($con);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发表博客</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>

    <!-- include summernote css/js -->
    <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>
    <style>
        .box {

            margin-top: 20px;
        }

        textarea.form-control {
            height: 450px;
        }


    </style>
</head>
<body>
<div class="container box">
    <a href="blog_list.php">返回</a>
    <form class="form-horizontal" action="blog_add.php" method="post">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="title" name="title" placeholder="输入标题名称">
            </div>
        </div>
        <div class="form-group">
            <label for="content" class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
                <textarea name="content" id="content" class="form-control"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">所属类别</label>
            <div class="col-sm-10">
                <select name="blogtype_id" id="" class="form-control">
                    <?php
                    foreach ($type_list as $key => $value) { ?>
                        <option value="<?php echo $value['type_id'] ?>"><?php echo $value['type_name'] ?></option>
                    <?php }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-10">
                <button type="submit" class="btn btn-default">保存</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>
<script>
    $(document).ready(function() {
        $('#content').summernote({
            height: 300,                 // set editor height
            minHeight: null,             // set minimum height of editor
            maxHeight: null,             // set maximum height of edito

        });
    });
</script>